package com.cskaoyan.catmall.controller.wx;

import com.cskaoyan.catmall.model.bo.AddGoodToCartBo;
import com.cskaoyan.catmall.model.bo.CartGoodStatus;
import com.cskaoyan.catmall.model.bo.ProductBo;
import com.cskaoyan.catmall.model.bo.UpdateProductBo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.CheckVo;
import com.cskaoyan.catmall.model.vo.czx.wx.CartVo;
import com.cskaoyan.catmall.service.czx.wx.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 逍遥子
 * @Date: 2022/01/11/9:19
 * @Description:
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    @GetMapping("index")
    public BaseRespVo index() {
        CartVo cartVo = wxCartService.selectCart();
        return BaseRespVo.ok(cartVo);

    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody ProductBo productBo) {

        wxCartService.deleteProduct(productBo);
        return BaseRespVo.ok();

    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody UpdateProductBo updateProductBo) {


        Subject subject = SecurityUtils.getSubject();

        wxCartService.updateProduct(updateProductBo);

        return BaseRespVo.ok();

    }

    @PostMapping("add")
    public BaseRespVo add(@RequestBody AddGoodToCartBo addGoodToCartBo) {

        wxCartService.insertProductToCart(addGoodToCartBo);
        return BaseRespVo.ok();

    }


    @GetMapping("goodscount")
    public BaseRespVo goodsCount() {
        int goodNum = wxCartService.selectCartGoodNum();
        return BaseRespVo.ok(goodNum);
    }

    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartGoodStatus cartGoodStatus) {

        CartVo cartVo = wxCartService.updateCartGoodStatus(cartGoodStatus.getProductIds()[0], cartGoodStatus.getIsChecked());
        return BaseRespVo.ok(cartVo);

    }


    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody AddGoodToCartBo addGoodToCartBo) {

        int goodsNum = wxCartService.fastAddGoodToCart(addGoodToCartBo);
        return BaseRespVo.ok(goodsNum);

    }


    @GetMapping("checkout")
    public BaseRespVo checkout(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId) {

        CheckVo checkVo = wxCartService.checkStatus(addressId, couponId, userCouponId);

        return BaseRespVo.ok(checkVo);

    }

}
